<?php
/*回款计划*/
class Payment_plan_model extends CI_Model {
	public function __construct() {
		parent :: __construct();
	}

	public function count_nums($name = ''){ /*取得总行数*/
			$user_id = $this->session->userdata('id');
			$role = $this->session->userdata('role');
			$this->db->select('*')->from('payment_plan')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
			if($role == '市场'){
				$this->db->where('payment_plan.owner_id',$user_id);
			}
			$count = $this->db->count_all_results();
			return $count;
	}

	public function list_payment_plans($limit,$offset = 0){ /*获取分页payment_plan数据*/
			$user_id = $this->session->userdata('id');
			$role = $this->session->userdata('role');
			$condition = 'contract.subject as contract_subject,contract.contract_id as contract_id,customer.customer_name as customer_name,payment_plan.*,user.user_name as user_name';
			$this->db->select($condition)->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
			if($role == '市场'){
				$this->db->where('payment_plan.owner_id',$user_id);
			}
			$this->db->limit($limit,$offset);
			$query = $this->db->get();
			$result = $query->result();
			/*查看合同是否已经全部回款*/
// 			$record_money=0;
// 			$record_nums=0;
// 			foreach($result as $row){
// 				$id = $row->id;
// 				$contract_id = $row->contract_id;
// 				$stages = $row->stages;
// 				$query = $this->db->query("SELECT money FROM payment_record where contract_id = $row->contract_id");
// 				foreach ($query->result() as $row_record){
// 					$record_money += $row_record->money; /*回款金额*/
// 					$record_nums++;
// 				}
				
// 				if($record_money >= ($row->money) ){ /*以回款&& $stages == $record_nums*/
// 					$data = array(
// 							'back' => 1,
// 					);
// 					$this->db->where('id', $id);
// 					$this->db->update('payment_plan', $data);
					
// 					$data = array(
// 							'payment_amount' => $record_money,
// 							'status' => '完成',
// 					);
// 					$this->db->where('contract_id', $contract_id);
// 					$this->db->update('contract', $data);
// 				}else{
// 					$data = array(
// 							'back' => 0,
// 					);
// 					$this->db->where('id', $id);
// 					$this->db->update('payment_plan', $data);
						
// 					$data = array(
// 							'payment_amount' => $record_money,
// 							'status' => '执行中',
// 					);
// 					$this->db->where('contract_id', $contract_id);
// 					$this->db->update('contract', $data);
// 				}
// 				$record_nums=0;
// 			}
			
			$this->db->select($condition)->from('payment_plan')->join('contract', 'payment_plan.contract_id = contract.contract_id')->join('customer', 'payment_plan.customer_id = customer.id')->join('user','payment_plan.owner_id=user.id');
			if($role == '市场'){
				$this->db->where('payment_plan.owner_id',$user_id);
			}
			$this->db->limit($limit,$offset);
			$this->db->order_by('payment_plan.create_time', "desc");
			$query = $this->db->get();
			$result = $query->result();
			
			return $result; 
	}
}
?>
